package top.fzqblog.action;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import top.fzqblog.Utils.R;
import top.fzqblog.exception.BizException;
import top.fzqblog.model.Prize;
import top.fzqblog.model.User;
import top.fzqblog.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by 抽离 on 2017/9/1 0001.
 */
@Controller
public class PrizeAction extends BaseAction{

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("getPrizeList")
    public R getPrizeList(HttpSession session, String prizeName, int page, int limit){
        if(getCurrentUser(session) == null){
            return R.error();
        }
        Page<Prize> p = new Page<Prize>();
        p.setCurrent(page);
        p.setSize(limit);
        Page<Prize> prizePage = new Prize().selectPage(p, new EntityWrapper<Prize>().like("prize_name", prizeName).orderBy("create_time", false));
        return R.ok().put("count", prizePage.getTotal()).put("data", prizePage.getRecords());
    }

    @RequestMapping("prizeList")
    public String prizeList(HttpSession session){
        if(getCurrentUser(session) != null){
            return "/system/prize_list";
        }
        return "login";
    }

    @RequestMapping("prizeEdit")
    public String prizeEdit(HttpSession session, Model model, Integer prizeId){
        System.out.println("prizeId：" + prizeId);
        if(prizeId != null){
            model.addAttribute("pageFlag", "updatePage");
            model.addAttribute("prize", new Prize().selectById(prizeId));
        }
        if(getCurrentUser(session) != null){
            return "/system/prize_edit";
        }
        return "login";
    }

    @RequestMapping("addPrize")
    public String addPrize(HttpSession session, Model model){
        if(getCurrentUser(session) != null){
            model.addAttribute("pageFlag", "addPage");
            return "/system/prize_edit";
        }
        return "login";
    }

    @ResponseBody
    @RequestMapping("upload")
    public R upload(MultipartFile file, HttpServletRequest request){
        String path = request.getSession().getServletContext().getRealPath("upload");
        String fileName = new Date().getTime()+".jpg";
        File newFile = new File(path, fileName);
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("上传出错");
        }
        String returnPath = request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+ request.getContextPath() + "/upload/" + fileName;
        return R.ok(returnPath);
    }

    @ResponseBody
    @RequestMapping("savePrize")
    public R savePrize(HttpSession session, Prize prize){
        if(getCurrentUser(session) == null){
            return R.error("请先登录！");
        }
        if(prize != null && prize.getPrizeId() != null){
            prize.updateById();
        }  else if(prize != null){
            prize.setCreateTime(new Date());
            prize.insert();
        }
        return R.ok();
    }

    @RequestMapping("wxPrizeList")
    public String wxPrizeList(HttpSession session, Model model){
        User user = (User) getCurrentWxUser(session);
        List<Prize> list = new Prize().selectList(new EntityWrapper<Prize>().eq("status", 1));
        model.addAttribute("list", list);
        model.addAttribute("user", user);
        return "/wx/prizelist";
    }

    @RequestMapping("duijiang")
    @ResponseBody
    public  R duijiang(HttpSession session, Integer id){
       if(!checkWxLogin(session)){
           return R.error("请先登录！");
       }
       User user = getCurrentWxUser(session);
        try {
            userService.duijiang(user, id);
        } catch (BizException e) {
            return R.error(e.getLocalizedMessage());
        }
        return R.ok();
    }


}
